package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.EXDELoginInfo;
import com.sl.au.entity.PartBaseInfo;
import com.sl.au.entity.PartCondition;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.EXDELoginInfoRepository;
import com.sl.au.repository.PartConditionRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.ModuleService;
import com.sl.au.service.PartConditionService;

@Service("partConditionService")
@Transactional
public class PartConditionServiceImpl extends BaseServiceImpl implements PartConditionService {

	@Autowired
	private PartConditionRepository partConditionRepository;
	
	
	@Override
	public PartConditionRepository getRepository() {
		return this.partConditionRepository;
	}


	@Override
	public List<PartCondition> getPage(int start, int length, String partId, String key, String orderName,
			Direction dir) {
		Page<PartCondition> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = this.partConditionRepository.getPageByPartAndKey(partId, key, pageable);
		} else {
			pages = this.partConditionRepository.getPageByPart(partId, pageable);
		}

		return pages.getContent();
	}


	@Override
	public int getCount(String partId, String key) {
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		int count = 0;
		if (StringUtils.hasText(key)) {
			count = this.partConditionRepository.getPageByPartAndKey(partId, key, pageable).getContent().size();
		} else {
			count = this.partConditionRepository.getPageByPart(partId, pageable).getContent().size();
		}
		return count;
	}


}
